<template>
  <div class="app-container">
    <div class="filterDiv">
      <el-input v-model="listQuery.routeName" size="small" placeholder="线路代码/名称" style="width: 200px;" class="filter-item" />
      <el-button size="small" type="primary" round @click="handleFilter">查询</el-button>
      <el-button v-permiss="['60204680']" style="float:right" size="small" type="primary" round @click="showImport">导入</el-button>
      <el-button v-permiss="['60204620']" style="float:right" size="small" type="primary" round @click="handleAdd">新增</el-button>
    </div>
    <div class="contentDiv">
      <el-table :key="tableKey" v-loading="listLoading" :data="tableData" fit highlight-current-row style="width: 100%;" size="small">
        <el-empty slot="empty" :image-size="120" description="暂无数据" />
        <el-table-column label="序号" type="index" width="50" align="center" />
        <el-table-column label="线路代码" align="center">
          <template slot-scope="{row}">
            <span>{{ row.routeCode }}</span>
          </template>
        </el-table-column>
        <el-table-column label="线路名称" align="center">
          <template slot-scope="{row}">
            <span>{{ row.routeName }}</span>
          </template>
        </el-table-column>
        <el-table-column label="备注" align="center">
          <template slot-scope="{row}">
            <span>{{ row.remarks }}</span>
          </template>
        </el-table-column>
        <el-table-column label="是否启用" align="center" width="120">
          <template slot-scope="{row}">
            <el-switch v-model="row.isDisable" :active-value="0" :inactive-value="1" @change="updateStatus($event, row)" />
          </template>
        </el-table-column>
        <el-table-column label="操作" align="center" width="200">
          <template slot-scope="{row}">
            <el-button v-permiss="['60204630']" type="text" size="small" @click="handleCompile(row)">编辑</el-button>
            <el-button v-permiss="['60204660']" type="text" size="small" @click="handleDel(row.id)">删除</el-button>
            <el-button v-permiss="['60204670']" type="text" @click="handleAssign2(row)">分配客户</el-button>
          </template>
        </el-table-column>
      </el-table>
      <pagination v-show="total>0" :total="total" :page.sync="listQuery.page" :limit.sync="listQuery.limit" @pagination="getList" />
    </div>
    <el-dialog :close-on-click-modal="false" :title="dialogStatus=='create'?'新增线路':'修改线路'" :visible.sync="dialogVisible" width="460px">
      <el-form ref="dataForm" :rules="rules" :model="form" label-position="right" label-width="80px" style="width: 400px; margin-left:10px;">
        <el-form-item label="线路代码" prop="routeCode">
          <el-input v-model="form.routeCode" placeholder="线路代码" />
        </el-form-item>
        <el-form-item label="线路名称" prop="routeName">
          <el-input v-model="form.routeName" placeholder="线路名称" />
        </el-form-item>
        <el-form-item label="备注" prop="remarks">
          <el-input v-model="form.remarks" placeholder="备注" />
        </el-form-item>
        <el-form-item label="是否启用" prop="isDisable">
          <el-radio v-model="form.isDisable" :label="0">是</el-radio>
          <el-radio v-model="form.isDisable" :label="1">否</el-radio>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer" align="center">
        <el-button @click="dialogVisible = false">取消</el-button>
        <el-button type="primary" @click="dialogStatus == 'create'?handleCreate():handleModify()">确定</el-button>
      </div>
    </el-dialog>
    <import-data ref="importRef" type="route" title="线路" />
    <assignCust ref="custTable" :tit="handleObj.routeName + handleObj.routeCode" :show-modal.sync="showModal" type="route" :handle-obj="handleObj" @handleAssign="handleAssignCust" />
  </div>
</template>
<script>
import { getRoute, saveRoute, delRoute, updateRouteDisabled, updateRouteIdByCustIdList } from '@/api/basedata'
import assignCust from '@/components/assignCust'
import importData from './importData'
export default {
  name: 'BaseRoute',
  components: { assignCust, importData },
  data() {
    return {
      tableKey: 0,
      tableData: [],
      showModal: false,
      handleObj: {},
      total: 0,
      listLoading: true,
      listQuery: {
        page: 1,
        routeName: '',
        routeCode: ''
      },
      showReviewer: false,
      form: {
        id: '',
        routeCode: '',
        routeName: '',
        remarks: '',
        isDisable: 0
      },
      resetform: {
        id: '',
        routeCode: '',
        routeName: '',
        remarks: '',
        isDisable: 0
      },
      dialogVisible: false,
      dialogVisible2: false,
      dialogStatus: '',
      rules: {
        routeName: [{ required: true, message: '名称不能为空', trigger: 'change' }],
        routeCode: [{ required: true, message: '代码不能为空', trigger: 'change' }]
      }
    }
  },
  created() {
    this.getList()
  },
  methods: {
    showImport() {
      this.$refs.importRef.option = {
        show: true
      }
    },
    handleAssign2(row) {
      this.showModal = true
      this.handleObj = row
    },
    handleAssignCust(arr) {
      var obj = {
        routeId: this.handleObj.id,
        custIdList: arr
      }
      updateRouteIdByCustIdList(obj).then(res => {
        if (res.data.errorCode === '0') {
          this.$refs.custTable.closeModal()
          this.$message.success(res.data.msg)
        } else {
          this.$refs.custTable.closeModal()
        }
      })
    },
    getList() {
      this.listLoading = true
      getRoute(this.listQuery).then(resp => {
        this.listLoading = false
        this.tableData = resp.data.data
      }).catch(() => {
        this.listLoading = false
      })
    },
    handleAdd(obj) {
      this.dialogVisible = true
      this.dialogStatus = 'create'
      for (const key in this.form) {
        this.form[key] = this.resetform[key]
      }
      this.$nextTick(() => {
        this.$refs['dataForm'].clearValidate()
      })
    },
    handleCompile(obj) {
      this.dialogVisible = true
      this.dialogStatus = 'update'
      for (const key in this.form) {
        this.form[key] = obj[key]
      }
      this.$nextTick(() => {
        this.$refs['dataForm'].clearValidate()
      })
    },
    handleFilter() {
      this.listQuery.page = 1
      this.getList()
    },
    updateStatus(e, row) {
      this.changeAvaiable(e, row.id)
    },
    changeAvaiable(isDisable, id) {
      var obj = { isDisable, id }
      updateRouteDisabled(obj).then(res => {
        if (res.data.errorCode === '0') {
          this.$message.success(res.data.msg)
        }
      })
    },
    handleDel(id) {
      this.$confirm('确定删除？', '提示', {
        confirmButtonText: '确定',
        closeOnClickModal: false,
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        this.delItem(id)
      })
    },
    handleModify() {
      this.$refs['dataForm'].validate((valid) => {
        if (valid) {
          saveRoute(this.form).then(res => {
            if (res.data.errorCode === '0') {
              this.getList()
              this.dialogVisible = false
              this.$message.success(res.data.msg)
            }
          })
        }
      })
    },
    handleCreate() {
      this.$refs['dataForm'].validate((valid) => {
        if (valid) {
          saveRoute(this.form).then(res => {
            if (res.data.errorCode === '0') {
              this.getList()
              this.dialogVisible = false
              this.$message.success(res.data.msg)
            }
          })
        }
      })
    },
    delItem(id) {
      delRoute(id).then(res => {
        if (res.data.errorCode === '0') {
          this.getList()
          this.dialogVisible = false
          this.$message.success(res.data.msg)
        }
      })
    }
  }
}
</script>
<style scoped>
.filter-item {
  position: relative;
}
/deep/.el-form-item {
  margin-bottom: 18px;
}
</style>
